package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.excel.BorrowExcel;
import com.example.springboot.controller.request.BorrowPageRequest;
import com.example.springboot.entity.Borrow;
import com.example.springboot.entity.Return;
import com.example.springboot.service.IBorrowService;
import com.example.springboot.utils.ExcelImportUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/borrow")
public class BorrowController {

    @Autowired
    IBorrowService borrowService;

    @PostMapping("/save")
    public Result save(@RequestBody Borrow obj) {
        borrowService.save(obj);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Borrow obj) {
        borrowService.update(obj);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        borrowService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("/deleteBatch")
    public Result delete(@RequestBody List<Integer> ids) {
        for (Integer id : ids) {
            borrowService.deleteById(id);
        }
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Borrow obj = borrowService.getById(id);
        return Result.success(obj);
    }

    @GetMapping("/list")
    public Result list() {
        List<Borrow> list = borrowService.list();
        return Result.success(list);
    }

    @GetMapping("/page")
    public Result page(BorrowPageRequest pageRequest) {
        return Result.success(borrowService.page(pageRequest));
    }

    @GetMapping("/pageRetur")
    public Result pageRetur(BorrowPageRequest pageRequest) {
        return Result.success(borrowService.pageRetur(pageRequest));
    }

    @PostMapping("/saveRetur")
    public Result saveRetur(@RequestBody Return obj) {
        borrowService.saveRetur(obj);
        return Result.success();
    }

    @DeleteMapping("/deleteRetur/{id}")
    public Result deleteRetur(@PathVariable Integer id) {
        borrowService.deleteReturById(id);
        return Result.success();
    }

    //  timeRange: week month month2 month3
    @GetMapping("/lineCharts/{timeRange}")
    public Result lineCharts(@PathVariable String timeRange) {
        return Result.success(borrowService.getCountByTimeRange(timeRange));
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        List<BorrowExcel> borrowList = ExcelImportUtil.importExcel(file, BorrowExcel.class);
        for (BorrowExcel borrowExcel : borrowList) {
            // 定义日期格式，只提取日期部分
//            DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
            // 定义只提取日期部分的格式化器
            DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy/M/d");
            // 进行转换
            String borrowExcelReturnDate = borrowExcel.getReturnDate();
            String createtime = borrowExcel.getCreatetime();
            String updatetime = borrowExcel.getUpdatetime();

            LocalDate returnTime = null;
            LocalDate createTime = null;
            LocalDate updateTime = null;
            if (borrowExcelReturnDate != null){
                returnTime = LocalDate.parse(borrowExcelReturnDate.substring(0, borrowExcelReturnDate.indexOf(' ')), dateFormatter);
            }
            if (createtime != null){
                createTime = LocalDate.parse(createtime.substring(0, createtime.indexOf(' ')), dateFormatter);
            }
            if (updatetime != null){
                updateTime = LocalDate.parse(updatetime.substring(0, updatetime.indexOf(' ')), dateFormatter);
            }
            Borrow borrow = new Borrow();
            BeanUtils.copyProperties(borrowExcel, borrow);
            borrow.setReturnDate(returnTime);
            borrow.setCreatetime(createTime);
            borrow.setUpdatetime(updateTime);
            borrowService.saveUpload(borrow);
        }
        return Result.success();
    }
}
